﻿/**
* Masque Window Library 0.1.2
* Copyright(c) 2008,
* @author Lookingon Team
*/
/// <reference path="LM_Base.js" />
if (Masque && !Masque.W) {
    Masque.W = {

        /**
        * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
        *
        * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
        *
        * Gets the full width/height because it's different for most browsers.
        */

        getBrowserWindowSize: function() {
            var de = document.documentElement;
            return {
                'width': (window.innerWidth || (de && de.clientWidth) || document.body.clientWidth),
                'height': (window.innerHeight || (de && de.clientHeight) || document.body.clientHeight)
            }
        },
        getViewportHeight: function() {
            if (window.innerHeight != window.undefined)
                return window.innerHeight;
            if (document.compatMode == 'CSS1Compat')
                return document.documentElement.clientHeight;
            if (document.body)
                return document.body.clientHeight;

            return window.undefined;
        },
        getViewportWidth: function() {
            var offset = 17;
            var width = null;
            if (window.innerWidth != window.undefined)
                return window.innerWidth;
            if (document.compatMode == 'CSS1Compat')
                return document.documentElement.clientWidth;
            if (document.body)
                return document.body.clientWidth;
        },

        /**
        * Gets the real scroll top
        */
        getScrollTop: function() {
            if (self.pageYOffset) // all except Explorer
            {
                return self.pageYOffset;
            }
            else
                if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
            {
                return document.documentElement.scrollTop;
            }
            else
                if (document.body) // all other Explorers
            {
                return document.body.scrollTop;
            }
        },
        getScrollLeft: function() {
            if (self.pageXOffset) // all except Explorer
            {
                return self.pageXOffset;
            }
            else
                if (document.documentElement && document.documentElement.scrollLeft) // Explorer 6 Strict
            {
                return document.documentElement.scrollLeft;
            }
            else
                if (document.body) // all other Explorers
            {
                return document.body.scrollLeft;
            }
        },
        FocusWindow: function(windowName, js) {
            if (windowName) {
                var o = document.createElement("a");
                o.setAttribute("target", windowName);
                o.setAttribute("href", "javascript:window.focus();" + (js ? js : ''));
                o.setAttribute("style", "display:none");
                document.body.appendChild(o);

                if (o.click) {
                    o.click();
                }
                else if (o.fireEvent) {
                    o.fireEvent('onclick');
                }
                else if (document.createEvent) {
                    var evt = document.createEvent("MouseEvents");
                    evt.initEvent("click", true, true);
                    o.dispatchEvent(evt);
                }



                o.parentNode.removeChild(o);
            }
        },
        FollowMessage: function(mark, target, msg, dTime, style) {
            var fm = $(mark);
            if (fm == null) {
                fm = document.createElement("div");
                fm.id = mark;
                document.body.appendChild(fm);
                if (Masque.V.isString(style))
                    fm.className = style;
            }
            else {
                fm.style.display = "";
            }
            if (!window.TW)
                window.TW = {};
            if (!window.TW.FollowMessage)
                window.TW.FollowMessage = {};
            var control = eval("window.TW.FollowMessage." + fm.id);

            fm.innerHTML = msg;
            Masque.L.ShowAgainst($(target), fm, Masque.L.PositionDirection.Right);
            if (!control) {
                if (Masque.V.isUndefined(dTime) || dTime < 0) {
                    dTime = 3000;
                }
                control = new Masque.Dis.AutoHidden(fm, dTime, 10, 10);
            }
            else {
                control.Reset();
            }

        },
        InlayMessage: function(object, msg, dTime) {
            object = $(object);
            object.innerHTML = msg;
            object.style.display = "";
            if (dTime > 0) {
                if (!window.TW)
                    window.TW = {};
                if (!window.TW.InlayMessage)
                    window.TW.InlayMessage = {};

                if (!window.TW.InlayMessage[object.id]) {
                    window.TW.InlayMessage[object.id] = new Masque.Dis.AutoHidden(object, dTime, 10, 10);
                }
                else {
                    window.TW.InlayMessage[object.id].Reset();
                }
            }


        },
        GetVisibleHeight: function(obj) {
            obj = $(obj);
            if (obj != null) {
                if (obj.clientHeight == 0)
                    return obj.offsetHeight;
                else
                    return obj.clientHeight
            }
        },
        GetVisibleWidth: function(obj) {
            obj = $(obj);
            if (obj != null) {
                if (obj.clientWidth == 0)
                    return obj.offsetWidth;
                else
                    return obj.clientWidth
            }

        }


    };
}
//对<a>进行处理，达到将页面在指定窗口打开的功能。
Masque.W.SW = {
    __Prefix: "Masque_W_SW_",
    GetName: function(id) {
        return this.__Prefix + id;
    },
    Check: function(a, wHeight, wWidth) {//a，要进行处理的a元素。wHeight、wWidth如果需要控制窗口大小，请指定。
        if (!a.target) {
            a.target = Math.random();
        }
        if (getCookie(this.GetName(a.target)) != "opened") {
            setCookie(this.GetName(a.target), "opened", null, "/", cookiesDomain);
            if (wHeight && wWidth) {
                if (a.target) {
                    window.open(a.href, this.GetName(a.target), 'height=' + wHeight + ', width=' + wWidth + ', top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
                    return false;
                }
            }
        }
        return true;
    }
}
Masque.W.MessageBox = Class();
Masque.W.MessageBox.extend({
    Type: { Layer: 0, Alert: 1, Confirm: 2 },
    initialize: function(BoxLayer, BoxType, options) {
        var me = this;
        this.option = {};
        this.BoxLayer = $(BoxLayer);
        this.SetOption(options);
        this.__bindEvent();
    },
    Show: function(options) {
        this.SetOption(options);
        if (options) {
            if (options.TitleText != null)
                $(this.option.TitleBox).innerHTML = options.TitleText;
            if (options.ContentText != null)
                $(this.option.ContentBox).innerHTML = options.ContentText;

        }

        if (Masque.V.isFunction(this.option.ShowBox))
            this.option.ShowBox();
        else {
            Element.showModal(this.BoxLayer);
        }
    },
    __bindEvent: function() {//绑定事件
        var me = this;
        if (me.option.ConfirmButton != null) {
            addEvent(me.option.ConfirmButton, "click", me.__ConfirmButtonEvent.bind(this));
        }
        if (me.option.CancelButton != null) {
            addEvent(me.option.CancelButton, "click", me.__CancelButtonEvent.bind(this));
        }
        if (me.option.CloseButton != null) {
            addEvent(me.option.CloseButton, "click", me.__CloseButtonEvent.bind(this));
        }
    },
    __ConfirmButtonEvent: function() {
        var me = this;

        var canClose = true;
        if (Masque.V.isFunction(me.option.ConfirmCallback)) {
            canClose = me.option.ConfirmCallback();
        }
        if (canClose != false) {
            me.Hidden();
        }
    }
    ,

    __CancelButtonEvent: function() {

        var me = this;

        var canClose = true;
        if (Masque.V.isFunction(me.option.CancelCallback)) {
            canClose = me.option.CancelCallback();
        }
        if (canClose != false) {
            me.Hidden();
        }
    }
    ,
    __CloseButtonEvent: function() {
        var me = this;
        var canClose = true;
        if (Masque.V.isFunction(me.option.CloseCallback)) {
            canClose = me.option.CloseCallback();
        }
        if (canClose != false) {
            me.Hidden();
        }
    },
    Hidden: function() {
        Element.hidePopup();
        if (Masque.V.isFunction(this.CloseCallback)) {
            this.CloseCallback();
        }
    },
    __RemoveEvent: function() { //这个功能还不能用，因为 事件 对像不对啊·
        var me = this;

        if (me.option.ConfirmButton != null) {
            removeEvent(me.option.ConfirmButton, "click", me.__ConfirmButtonEvent);
        }
        if (me.option.CancelButton != null) {
            removeEvent(me.option.CancelButton, "click", me.__CancelButtonEvent);
        }
        if (me.option.CloseButton != null) {
            removeEvent(me.option.CloseButton, "click", me.__CloseButtonEvent);
        }
    },
    option: {
        ConfirmButton: null,
        CancelButton: null,
        CloseButton: null,
        ConfirmCallback: null,
        CancelCallback: null,
        CloseCallback: null,
        ShowBox: null,
        TitleBox: null,
        TitleText: "系统提示",
        ContentBox: null,
        ContentText: ""
    }


});



